package in.iqing.view.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import java.util.List;

import butterknife.Bind;
import in.iqing.app.R;
import in.iqing.base.BaseFragment;
import in.iqing.control.adapter.RecommendAdapter;
import in.iqing.control.api.ApiHelper;
import in.iqing.control.api.callback.RecommendCallback;
import in.iqing.control.helper.IntentHelper;
import in.iqing.control.helper.LogHelper;
import in.iqing.model.bean.Recommend;
import in.iqing.view.activity.RecommendBookListActivity;
import in.iqing.view.widget.ListSpacesItemDecoration;

/**
 * Created by ZuoShu on 15/7/3.
 */
public class RecommendFragment extends BaseFragment {
    @Bind(R.id.recommend_list)
    RecyclerView recommendRecycler;
    RecommendAdapter recommendAdapter;

    private ExtRecommendsCallback callback;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_recommend, null);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        initRecommendRecyclerView();
    }

    private void loadRecommends() {
        showLoading();
        callback = new ExtRecommendsCallback();
        ApiHelper.getSingleton().getRecommend(VOLLEY_TAG, callback);
    }

    private void initRecommendRecyclerView() {
        recommendAdapter = new RecommendAdapter(getContext());
        recommendRecycler.setLayoutManager(new LinearLayoutManager(getContext()));
        recommendRecycler.setHasFixedSize(false);
        recommendRecycler.addItemDecoration(
                new ListSpacesItemDecoration(getResources().getDimensionPixelSize(R.dimen.recommend_list_spacing))
        );
        recommendRecycler.setAdapter(recommendAdapter);
        recommendAdapter.setListener(new ExtOnRecommendClickListener());
    }

    @Override
    public void onResume() {
        super.onResume();
        loadRecommends();
    }

    private class ExtRecommendsCallback extends RecommendCallback {

        @Override
        public void onSuccess(List<Recommend> recommends) {
            if (recommends == null || recommends.size() == 0) {
                showEmpty();
                return;
            }
            recommendAdapter.setRecommends(recommends);
            recommendAdapter.notifyDataSetChanged();
            showContent();
        }

        @Override
        public void onFailure(int code, String msg) {
            showEmpty();
            LogHelper.warn(TAG, "load recommend error code:" + code + " msg:" + msg);
        }
    }

    private class ExtOnRecommendClickListener implements RecommendAdapter.OnRecommendClickListener {

        @Override
        public void onRecommendClick(Recommend recommend) {
            Bundle data = new Bundle();
            data.putSerializable("recommend", recommend);
            IntentHelper.startActivity(RecommendFragment.this.getActivity(), RecommendBookListActivity.class, data);
        }
    }
}
